/**
 * 二级分类添加修改
 * @author duanzuocai
 * @since 2018/10/18
 */
layui.define(['form', 'layedit', 'laydate', 'upload'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var layedit = layui.layedit;
    var laydate = layui.laydate;
    let upload = layui.upload;
    let timestamp = new Date().getTime();

    form.render(null, "zmn-form");

    // 自定义验证规则
    form.verify({
        name: function(value){
            if(value.length > 20){ return "分类名称长度超过20了，请调整后再试"; };
        },
        sort: function(value){
            if (!/^[1-9]\d*$/.test(value)) {
                return "排序值只能是正整数";
            }
            if (value.length > 8){
                return "排序字段长度不能超过8位";
            }
        },tariffName:function (value) {
            if(!/^[A-Za-z0-9\u4e00-\u9fa5]+$/.test(value)){
                return "计价器名称只能输入字母、数字、汉字";
            }
        },
        serviceMinutes: function(value){
            if (value&&value!=''&&!/^0$|^[1-9]\d*$/.test(value)) {
                return "服务时长只能是正整数";
            }
        }
    });

    reLoadImage("icon", json.categ.icon, "iconValue");
    //初始化文件上传插件
    uploaderImage("icon", "iconValue", "jpg|png|jpeg|gif", 1024);

    // 提交
    form.on('submit(zmn-form-submit)', function(obj){
        if(!obj.field.tariffStatus){
            layer.msg("计价器状态必选");
            return false;
        }
        var openId = layer.open({type: 3});
        var data = obj.field;
        data['parentId'] = json.parentId;
        data['showType'] = 2;
        var url = "/showCateg/addEdit.action";

        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            success:function(result) {
                layer.close(openId);
                if(result.status == 200) {
                    layer.msg(result.message,{time: 1000},()=>{
                        zmnLayUtil.closeThisIframeMsg(parent, result.message, "zmn-table");
                        parent.location.reload();
                    });
                } else {
                    layer.msg(result.message, {"icon": 2});
                }
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
                layer.close(openId);
            }
        })

        return false;
    });

    /**
     * 描述: 图片上传方法
     * @param elemId
     * @param valueId
     */
    let indexUploadImags;

    function uploaderImage(elemId, valueId, exts, size) {
        upload.render({
            elem: '#' + elemId
            , exts: exts
            , acceptMime: 'image/*'
            , accept: 'images'
            , url: '/product/picture/uploaderImage.action?timestamp=' + timestamp + '&elemId=' + elemId
            , size: size
            , before: function (obj) {
                indexUploadImags = layui.layer.load(1, {
                    shade: [0.5, '#DBDBDB']
                });
            }
            , done: function (res) {
                if (res.code == 2) {
                    var data = res.data;
                    $("#" + valueId).val(data.src);
                    $("#" + elemId).attr("style", "background:url( " + data.src + "?" + Date.now() + ");background-size:100% 100%;background-repeat:no-repeat;");
                    let next = $("#" + valueId).next();
                    let next1 = $(next).next();
                    $(next).attr("style", "visibility:hidden !important");
                    $(next1).attr("style", "visibility:hidden !important");
                } else {
                    layer.msg("上传失败！")
                }
                layui.layer.close(indexUploadImags);
            }
        });
    }

    function reLoadImage(elemId, value, valueId) {
        zmnUploadUtil.reLoadImage($, elemId, value, valueId);
    }

    //点击取消
    $(document).on('click', '#cancel', function () {
        let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index);
    });

    //对外暴露的接口
    exports('art_categ_show_detail2', {});
});

function deleteImg(element) {
    var elementValue = element+'Value';
    document.getElementById(element).removeAttribute('style');
    Array.from(document.getElementById(element).children).forEach(function(item){
        item.removeAttribute('style');
    });
    document.getElementById(elementValue).removeAttribute('value');
}